package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.constant.MessageConstant;
import com.itheima.entity.Result;
import com.itheima.pojo.OrderSetting;
import com.itheima.service.OrdersettinService;
import com.itheima.utils.POIUtils;

import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @Author: bowen
 * @Date: 2019/6/26 15:18
 * @Description:预约设置控制器
 */
@RestController
@RequestMapping("/ordersetting")
public class OrdersettingController {

    @Reference
    OrdersettinService ordersettinService;
    
    /**
     *  @描述：上传文件
     */
    @RequestMapping("/upload")
    public Result upload(@RequestParam("excelFile") MultipartFile multipartFile) {
        try {
            List<String[]> strings = POIUtils.readExcel(multipartFile);
            List<OrderSetting> orderSettingList =new ArrayList<>();
            for (String[] string : strings) {
                OrderSetting orderSetting = new OrderSetting();
                orderSetting.setOrderDate(new Date(string[0]));
                orderSetting.setNumber(Integer.parseInt(string[1]));
                orderSettingList.add(orderSetting);
            }
            ordersettinService.add(orderSettingList);
            return new Result(true, MessageConstant.UPLOAD_SUCCESS);
        } catch (IOException e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.UPLOAD_FAIL);
        }

    }
    /**
     *  @描述：获取预约数据
     */
    @RequestMapping("/getOrdersettingData")
    public Result getOrdersettingData(String date) {
        try {
            List<Map> ordersettings = ordersettinService.getOrdersettingData(date);
            return new Result(true,MessageConstant.GET_ORDERSETTING_SUCCESS,ordersettings);
        } catch (Exception e){
            e.printStackTrace();
            return new Result(false,MessageConstant.GET_ORDERSETTING_FAIL);

        }
    }

    /**
     *  @描述：根据日期设置可预约人数
     */
    @RequestMapping("/setData")
    public Result setData(@RequestBody OrderSetting orderSetting) {

        try {
            ordersettinService.setData(orderSetting);
        } catch(Exception e){
            e.printStackTrace();
            return new Result(false,MessageConstant.ORDERSETTING_FAIL);
        }
      return new Result(true,MessageConstant.ORDERSETTING_SUCCESS);
    }
}
